//
//  CollegeGradeCollectionViewController.m
//  Arts1
//
//  Created by 孤星晨宇 on 15/9/23.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//


#pragma mark ---- 成绩查询
#import "CollegeGradeCollectionViewController.h"
#import "CollegeGradeCollectionViewCell.h"
#import "AllHeader.h"
#import "RecruitModel.h"


//#define gradeURL @"http://arts1.dlab.com.cn/app/getmark"
#define gradeURL @"http://arts1.cn/app/getmark"



@interface CollegeGradeCollectionViewController ()

// 数据源
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation CollegeGradeCollectionViewController

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
     self.collectionView.backgroundColor = kBackgroundColor;
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    [self requestData];
    // Register cell classes
    self.collectionView.alwaysBounceVertical = YES;
    [self.collectionView registerClass:[CollegeGradeCollectionViewCell class] forCellWithReuseIdentifier:@"collegeGradeCell"];
    
    // Do any additional setup after loading the view.
}


//请求数据
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    // 请求所需参数  _spaceId
    NSDictionary *pardic = @{@"spaceId":_spaceId};
    
    [manage GET:gradeURL parameters:pardic
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
            // 请求成功的时候调用这个block
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            // 请求返回的状态，当status = 1 时有数据
            NSInteger status = [dic[@"status"] integerValue];
            if (status == 1) {
                NSArray *arr = dic[@"list"];
                for (NSDictionary *dict in arr) {
                    // 将数据装换成model类
                    RecruitModel *recruit = [[RecruitModel alloc] init];
                    [recruit setValuesForKeysWithDictionary:dict];
                    [self.dataArr addObject:recruit];
                }
            }
            NSLog(@"院校成绩查询 %@",dic);
            //解析完数据之后，在主线程中刷新数据
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            // 请求失败的时候调用调用这个block
            NSLog(@"请求失败");
        }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH - 20, 60);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollegeGradeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collegeGradeCell" forIndexPath:indexPath];
    
//    cell.layer.shadowOpacity = 0.5f;
//    cell.layer.shadowRadius = 1.0f;
//    cell.layer.shadowOffset = CGSizeMake(0,1);
    [UIView viewShadowColor:cell];
    
    cell.recruit = (RecruitModel *)_dataArr[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    RecruitModel *recruit = (RecruitModel *)_dataArr[indexPath.row];
    if (_schoolgrateBlock) {
        _schoolgrateBlock(recruit.Id);
    }
}


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
